//
//  rainSnow.m
//  RL_项目二
//
//  Created by rimi on 16/3/23.
//  Copyright © 2016年 ruifengpeixun. All rights reserved.
//

#import "RainSnow.h"

@implementation RainSnow

//初始化云
- (UIImageView *)cloud{
    if (!_cloud) {
        _cloud = [[UIImageView alloc]initWithFrame: CGRectMake(0, 0, self.bounds.size.width*0.84, self.bounds.size.height*0.54)];
        _cloud.image = [UIImage imageNamed:@"云"];
    }
    return _cloud;
}

//初始化雪
//- (UIImageView *)snow{
//    if (!_snow) {
//        _snow = [[UIImageView alloc]initWithFrame:CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)];
//
//
//    }
//    return _snow;
//}

//初始化雨
//- (UIImageView *)rain{
//    if (!_rain) {
//        _rain = [[UIImageView alloc]init];
//    }
//    return _rain;
//}

//雪的大小
- (NSInteger)degree{
    if (!_degree) {
        _degree = 150;
    }
    return _degree;
}

//重写初始化方法
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self start];
    }
    return self;
}
- (void)start{
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleAction:)];
    //调整雪的数量，数值越小雪越大
    self.displayLink.frameInterval = self.degree+200;
    [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    
    self.displayLink1 = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleAction1:)];
    //调整雪的数量，数值越小雪越大
    self.displayLink1.frameInterval = self.degree-60;
    [self.displayLink1 addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    
    //加载云
    double width = self.bounds.size.width;
    double height =self.bounds.size.height;
    self.cloud = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width*0.84, height*0.54)];
    self.backgroundColor = [UIColor whiteColor];
    self.cloud.image = [UIImage imageNamed:@"云"];
    [self addSubview:self.cloud];
    CAKeyframeAnimation * keyframe = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];
    keyframe.repeatCount = HUGE_VAL;
    keyframe.values = @[@(self.bounds.size.width*0.3),@(self.bounds.size.width*0.55),@(self.bounds.size.width*0.3)];
    keyframe.duration = 18;
    [_cloud.layer addAnimation:keyframe forKey:nil];
    
    _mask = [[UIView alloc]initWithFrame:CGRectMake(0, self.bounds.size.height*0.95, self.bounds.size.width, self.bounds.size.height*0.2)];
    _mask.backgroundColor = [UIColor whiteColor];
    [self addSubview:_mask];
}

- (void)handleAction:(CADisplayLink *)displayLink{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"雪"]];
    CGFloat scale = arc4random_uniform(10)/100.0+0.3;
    imageView.transform = CGAffineTransformMakeScale(scale, scale);
    CGSize winSize = self.bounds.size;
    CGFloat x = arc4random_uniform(winSize.width*0.7)+0.1*winSize.width;
    CGFloat y =  self.bounds.size.height*0.45;  //-imageView.frame.size.height
    imageView.center = CGPointMake(x, y);
    [self addSubview:imageView];
    //将控件提到最上层

    
    [UIView animateWithDuration:arc4random_uniform(5)+10 animations:^{
        CGFloat toX = arc4random_uniform(winSize.width*0.65)+winSize.width*0.2;
        CGFloat toY = imageView.frame.size.height * 0.5 + winSize.height;
        imageView.center = CGPointMake(toX, toY);
        imageView.transform = CGAffineTransformRotate(imageView.transform, arc4random_uniform(M_PI * 2));
        imageView.alpha = 0.6;
        imageView.transform = CGAffineTransformMakeScale(scale*1.1, scale*1.1);
    } completion:^(BOOL finished) {
        [imageView removeFromSuperview];
    }];
    
    [self bringSubviewToFront:self.cloud];
    [self bringSubviewToFront:self.mask];
}

- (void)handleAction1:(CADisplayLink *)displayLink{
    UIImageView *imageRain = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"雨滴"]];
    CGFloat scale = 0.2;
    imageRain.transform = CGAffineTransformMakeScale(scale, scale);
    CGSize winSize = self.bounds.size;
    CGFloat x = arc4random_uniform(winSize.width*0.5)+0.2*winSize.width;
    CGFloat y =  self.bounds.size.height*0.45;  //-imageView.frame.size.height
    imageRain.center = CGPointMake(x, y);
    [self addSubview:imageRain];
    [UIView animateWithDuration:arc4random_uniform(2)+3 animations:^{
        CGFloat toX = x;
        CGFloat toY = imageRain.frame.size.height * 0.5 + winSize.height;
        imageRain.center = CGPointMake(toX, toY);
        //        _rain.transform = CGAffineTransformRotate(imageView.transform, arc4random_uniform(M_PI * 2));
        imageRain.alpha = 0.6;
        imageRain.transform = CGAffineTransformMakeScale(scale*1.1, scale*1.1);
    } completion:^(BOOL finished) {
        [imageRain removeFromSuperview];
    }];
    [self bringSubviewToFront:self.cloud];
    [self bringSubviewToFront:self.mask];
}

//控件消失响应动画
- (void)disMiss{
    [UIView animateWithDuration:0.5 animations:^{
        self.transform = CGAffineTransformMakeScale(0.1, 0.1);
        self.alpha = 0;
    } completion:^(BOOL finished){
        [self removeFromSuperview];
    }];
}


@end
